package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.pojo.MarketingSettingValuePojo;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.MarketingSettingDao;
import com.xbongbong.saas.domain.entity.MarketingSettingEntity;
import com.xbongbong.saas.enums.MarketingManagementSettingEnum;
import com.xbongbong.saas.model.MarketingSettingModel;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

/**
 * @author bomfish
 */
@Service("marketingSettingModel")
public class MarketingSettingModelImpl implements MarketingSettingModel {
    @Resource
    private MarketingSettingDao marketingSettingDao;

    @Override
    public Long insert(MarketingSettingEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        entity.setAddTime(now);
        entity.setDel(0);
        return marketingSettingDao.insert(entity);
    }

    @Override
    public void insertBatch(List<MarketingSettingEntity> list) {
        marketingSettingDao.insertBatch(list);
    }

    @Override
    public List<MarketingSettingEntity> findEntities(Map<String, Object> param) {
        return marketingSettingDao.findEntities(param);
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return marketingSettingDao.getEntitiesCount(param);
    }

    @Override
    public void update(MarketingSettingEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        marketingSettingDao.update(entity);
    }

    @Override
    public MarketingSettingEntity getByKey(Long key, String corpid) {
        return marketingSettingDao.getByKey(key, corpid);
    }

    @Override
    public MarketingSettingEntity getByType(Integer settingType, String corpid) {
        return marketingSettingDao.getByType(settingType, corpid);
    }

    @Override
    public Map<Integer, MarketingSettingValuePojo> getByCorpid(String corpid) {
        List<MarketingSettingEntity> marketingSettingEntityList = marketingSettingDao.getByCorpid(corpid);
        Map<Integer, MarketingSettingValuePojo> marketingSettingMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (CollectionUtils.isNotEmpty(marketingSettingEntityList)) {
            for (MarketingSettingEntity marketingSettingEntity : marketingSettingEntityList) {
                String settingValue = marketingSettingEntity.getSettingValue();
                MarketingSettingValuePojo marketingSettingValuePojo = JSONObject.parseObject(settingValue, MarketingSettingValuePojo.class);
                marketingSettingMap.put(marketingSettingEntity.getSettingType(), marketingSettingValuePojo);
            }
        }
        for (MarketingManagementSettingEnum value : MarketingManagementSettingEnum.values()) {
            if (!marketingSettingMap.containsKey(value.getCode())) {
                marketingSettingMap.put(value.getCode(), MarketingSettingValuePojo.getByType(value.getCode()));
            }
        }
        return marketingSettingMap;
    }

}
